#include "unitframe.h"
#include "globals.h"
#include "functions.h"

UnitFrame::UnitFrame(int pid, float scale, int b)
{
    border = b;
    playerID = pid;

    icon.x = 8;
    icon.y = (box.h - icon.h) / 2 + box.y;
    icon.w = 32;
    icon.h = 32;

    if (playerID == 0)
    {
        box.x = 8 + icon.w;
        box.y = 8;
        box.w = 160 * scale;
        box.h = 40 * scale;
    }
    else if (playerID > 0)
    {
        box.x = 8 + icon.w;
        box.y = 8 + 16 + playerID * 48;
        box.w = 160 * scale;
        box.h = 40 * scale;
    }
    else if (playerID < 0)
    {
        // npc
    }

    hp.x = box.x + border;
    hp.y = box.y + border;

    mp.x = box.x + border;
    mp.y = box.h / 2 + box.y;
}

void UnitFrame::draw()
{
    if (box.w < 100)
    {
        box.w = 100;
    }

    if (box.h < 20)
    {
        box.h = 20;
    }

    icon.y = (box.h - icon.h) / 2 + box.y;

    hp.h = box.h / 2 - border;

    mp.y = box.h / 2 + box.y;
    mp.h = box.h / 2 - border;

    if (playerID == 0) // If player is myPlayer
    {
        if (P.get_hp() > P.get_maxhp())
        {
            hp.w = (box.w - border * 2);
        }
        else
        {
            hp.w = (box.w - border * 2) * ((float)P.get_hp() / P.get_maxhp());
        }

        if (P.get_mp() > P.get_maxmp())
        {
            mp.w = (box.w - border * 2);
        }
        else
        {
            mp.w = (box.w - border * 2) * ((float)P.get_mp() / P.get_maxmp());
        }

        hpStream << (int)P.get_hp() << "/" << P.get_maxhp();
        mpStream << (int)P.get_mp() << "/" << P.get_maxmp();
    }
    else
    {
        /*if (vPlayer[playerID - 1]->hp > vPlayer[playerID - 1]->maxhp)
        {
            hp.w = (box.w - border * 2);
        }
        else
        {
            hp.w = (box.w - border * 2) * ((float)vPlayer[playerID - 1]->hp / vPlayer[playerID - 1]->maxhp);
        }

        if (vPlayer[playerID - 1]->mp > vPlayer[playerID - 1]->maxmp)
        {
            mp.w = (box.w - border * 2);
        }
        else
        {
            mp.w = (box.w - border * 2) * ((float)vPlayer[playerID - 1]->mp / vPlayer[playerID - 1]->maxmp);
        }

        hpStream << (int)vPlayer[playerID - 1]->hp << "/" << vPlayer[playerID - 1]->maxhp;
        mpStream << (int)vPlayer[playerID - 1]->mp << "/" << vPlayer[playerID - 1]->maxmp;*/
    }

    SDL_FillRect(screen, &icon, SDL_MapRGB(screen->format, 128, 128, 128));
    SDL_FillRect(screen, &box, SDL_MapRGB(screen->format, 0, 0, 0));
    SDL_FillRect(screen, &hp, SDL_MapRGB(screen->format, 0, 255, 0));
    SDL_FillRect(screen, &mp, SDL_MapRGB(screen->format, 0, 0, 255));

    hpText = TTF_RenderText_Solid(font, hpStream.str().c_str(), textColor);
    mpText = TTF_RenderText_Solid(font, mpStream.str().c_str(), textColor);

    apply_surface((box.w - hpText->w) / 2 + box.x, (hp.h - hpText->h) / 2 + hp.y + 2, hpText, screen, NULL, false);
    apply_surface((box.w - mpText->w) / 2 + box.x, (mp.h - mpText->h) / 2 + mp.y + 2, mpText, screen, NULL, false);

    SDL_FreeSurface(hpText);
    SDL_FreeSurface(mpText);

    hpStream.str("");
    mpStream.str("");
}
